if ( !window.controller ) throw new Error( '"controller" not initialized' );
if(!controller.Dialog){
	controller.Dialog = (function(){
		var $ = controller;
		var EE_A = "add";
		var EE_C = "close";
		var EE_O = "open";
		
		
		
		
		var Dialog = function(app){
			//console.log(app);
			this._app = app;
			DialogSuperPrototype.constructor.call( this );
			this.__timer = new $.utils.Timer(200, 0);
			this.__timer.addEventListener("timer", this, openMessage);
			//this.__timer.start();
		}
		
		$.extend( Dialog, $.events.EventDispatcher );
		var DialogPrototype 		= Dialog.prototype,
			DialogSuperPrototype    = Dialog.superPrototype;
			
		var dispatchEvent= DialogSuperPrototype.dispatchEvent;
		var EEvent = $.events.Event;
		
		DialogPrototype._app = null;
        DialogPrototype._id = 0;
		
		DialogPrototype.open = false;
		DialogPrototype._pool = new Array();
	// Добавить новый диалог на отображение
	/**
	* @param title - Заголовсок сообщения
	* @param tect - jQuery.tmpl() объект или просто текст
	* @param type - Тип сообщения
	*/
		DialogPrototype.Add = function(title, text, type){
            this._app.find("div.message").remove();
            this._id += 1;
			var message = new $.utils.Hash();
			message.add("title", (title)? title: 0);
			message.add("text",  (text) ? text : 0);
			message.add("type",  (type) ? type : 0);
            message.add("id",  (this._id) ? type : 0);
			this._pool.push(message);
            
            var message = this._pool.shift();
            if (message.get("title") == "none" || message.get("title") == "null" || message.get("title") == null)
                this._app.append("<div class='message'>"+message.get("text")+"</div>");                
            else
                this._app.append("<div class='message'>"+message.get("title")+": "+message.get("text")+"</div>");                
            this._app.show();
            
            if (parseInt(message.get("type")) == 1)
                this._app.find("div.message").css({"color":"#FF0000"}).animate({"color":"#FFFFFF"}, { duration: 5000, queue: false }, function(){wj.dialog.close();});
                
            if (parseInt(message.get("type")) == 2)
            {
                this._app.find("div.message").css({"color":"#7CA23B"}).animate({"color":"#FFFFFF"}, { duration: 5000, queue: false }, function(){wj.dialog.close();});
            }
		}
			
		var openMessage = function(){
			//console.log("Попытка открыть диалог");
			if(this.open){
				return false;
			}
			if(this._pool.length > 0){
				this.open = true;
				var message = this._pool.shift();
				this._app.find("div.message").html(message.get("title")+": "+message.get("text"));                
				this._app.show();
                
                if (parseInt(message.get("type")) == 1)
                    this._app.find("div.message").css({"color":"#FF0000"}).animate({"color":"#FFFFFF"}, 5000, function(){wj.dialog.close();});
                    
                if (parseInt(message.get("type")) == 2)
                {
                    console.log("type 2");
                    this._app.find("div.message").css({"color":"#7CA23B"}).animate({"color":"#FFFFFF"}, 5000, function(){wj.dialog.close();});
                }
				
				//var event  = new EEvent(EE_O);
				//	event.data = this._pool.shift();
					//console.log("Открываем диалог: "+ message.get("title"));
				//	dispatchEvent.call(this, event);
			}
		}
		DialogPrototype.close = function(){
			if(this.open){
                this._app.find("div.message").html("");
				this.open = false;
			}
		}
		
	
		return Dialog;
	}());
}